package com.ilop.sthome.mvp.present.mine;

import android.content.Context;
import android.os.Handler;

import com.aliyun.iot.aep.sdk.apiclient.callback.IoTResponse;
import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.mvp.contract.mine.RoomContract;
import com.ilop.sthome.mvp.model.CommonModel;
import com.ilop.sthome.mvp.model.common.CommonModelImpl;
import com.example.common.mvp.onModelCallBack;
import com.ilop.sthome.ui.dialog.TipDialog;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

/**
 * @author skygge
 * @Date on 2020-02-11.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class RoomPresenter extends BasePresenterImpl<RoomContract.IView> implements RoomContract.IPresent {

    private Context mContext;
    private CommonModelImpl mModel;
    private RoomBean mRoom;
    private Handler mHandler;

    public RoomPresenter(Context mContext) {
        this.mContext = mContext;
        mHandler = new Handler();
        mModel = new CommonModel(mHandler);
    }

    @Override
    public void getRoomInfo(int position) {
        List<RoomBean> mRoomList = RoomDaoUtil.getInstance().findRoomNotEmptyAndDefault();
        mRoom = mRoomList.get(position);
        if (mRoom.getNickname()!=null){
            mView.showRoomName(mRoom.getNickname());
        }else {
            mView.showRoomName(ConfigureUtil.roomName(mContext, mRoom.getRid()));
        }
    }

    @Override
    public void onSaveRoom(String roomId, String name) {
        mModel.onCreateRoom(roomId, name, new onModelCallBack() {
            @Override
            public void onResponse(IoTResponse response) {
                if (response.getCode() == 200){
                    RoomBean mExistRoom= RoomDaoUtil.getInstance().getRoomByRoomId(roomId);
                    mExistRoom.setNickname(name);
                    mExistRoom.setRoom_name(name);
                    RoomDaoUtil.getInstance().getRoomDao().update(mExistRoom);
                    mHandler.post(()-> mView.doSuccess(mContext.getString(R.string.add_success)));
                }else{
                    mHandler.post(()-> mView.showToastMsg(response.getLocalizedMsg()));
                }
            }

            @Override
            public void onFailure(Exception e) {
                mHandler.post(()-> mView.showToastMsg(mContext.getString(R.string.failed)));
            }
        });
    }

    @Override
    public void onUpdateRoom(String name) {
        if (mRoom.getNickname() == null){
            onSaveRoom(mRoom.getRid(), name);
        }else {
            mModel.onUpdateRoomName(mRoom.getUserId(), name, new onModelCallBack() {
                @Override
                public void onResponse(IoTResponse response) {
                    if (response.getCode() == 200){
                        mRoom.setRoom_name(name);
                        mRoom.setNickname(name);
                        RoomDaoUtil.getInstance().getRoomDao().update(mRoom);
                        mHandler.post(()-> mView.doSuccess(mContext.getString(R.string.modify_successfully)));
                    }else{
                        mHandler.post(()-> mView.showToastMsg(response.getLocalizedMsg()));
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    mHandler.post(()-> mView.showToastMsg(mContext.getString(R.string.failed)));
                }
            });
        }
    }

    @Override
    public void onDeleteRoom() {
        List<DeviceInfoBean> mDevice = DeviceDaoUtil.getInstance().findDeviceIntoRoom(mRoom.getRid());
        if (mDevice.size() > 0){
            mView.showToastMsg(mContext.getString(R.string.not_allow_delete_room));
        }else {
            String sd = String.format(mContext.getResources().getString(R.string.want_to_delete_confirm_eq),
                    ConfigureUtil.roomName(mContext, mRoom.getRid()));
            TipDialog dialog = new TipDialog(mContext, () -> {
                if (mRoom.getNickname() == null){
                    mRoom.setRoom_name("");
                    RoomDaoUtil.getInstance().getRoomDao().update(mRoom);
                    mView.doSuccess(mContext.getString(R.string.delete_success));
                }else {
                    mModel.onDeleteRoom(mRoom.getUserId(), new onModelCallBack() {
                        @Override
                        public void onResponse(IoTResponse response) {
                            if (response.getCode() == 200) {
                                mRoom.setNickname(null);
                                mRoom.setRoom_name("");
                                RoomDaoUtil.getInstance().getRoomDao().update(mRoom);
                                mHandler.post(() -> mView.doSuccess(mContext.getString(R.string.delete_success)));
                            }else{
                                mHandler.post(()-> mView.showToastMsg(response.getLocalizedMsg()));
                            }
                        }

                        @Override
                        public void onFailure(Exception e) {
                            mHandler.post(() -> mView.showToastMsg(mContext.getString(R.string.failed)));
                        }
                    });
                }
            });
            dialog.setMsg(sd);
            dialog.show();
        }
    }
}
